'use strict'
;(function(){
    var dom = '<div class="subject-content" id="SUBJECT">\
                <div class="subject-top" id="subjectCont">\
                    <div class="subject-top-left">\
                        <h1></h1>\
                        <p></p>\
                    </div>\
                    <div class="subject-top-right" id="mainSubject"> \
                    </div>\
                </div>\
                <div class="subject-bottom" id="specialRecommend">\
                </div>\
            </div>'

    var home_page = {};
    /* 模版翻页处理 */
    var pages_info = {};
    var recommend_data;
    var totalPage = 0;
    var page = 0;
    //页面初始化
    home_page.init = function(){
        //获取一级列表信息
        home_page.get_special_data();     
    };
    /*
       专题数据
     */
    //获取专题数据
    home_page.get_special_data = function(data){  
        getApi(API_URL + 'meta/p3/data.json', function(data) {
            //解析专题数据
            var special_data = home_page.analysis_special_data(data);

            // 生成专题数据
            home_page.create_special(special_data.data);


            //解析专题页推荐位数据
            recommend_data = home_page.analysis_recommend_data(data);
            home_page.create_recommend(recommend_data);
        })
        //处理焦点
        //add_area_btn();
    };
    // 解析数据
    home_page.analysis_special_data = function(data){
        var special_data = {}
        special_data.data = data.data;
        return special_data;
    };

    // 生成专题数据
    home_page.create_special = function(special_data){
        
        // 专题背景设置
        $('#SUBJECT').css({"background":"url(http://127.0.0.1:8000/custom-templates/images/subject/ditu.png)"})

        var subject_t_l = '<h1>'+special_data.title+'</h1><p>'+special_data.long_desc+'</p>'
        var subject_t_r = '<img src="'+special_data.img+'"/><p>'+special_data.subtitle+'</p>'
        $('.subject-content .subject-top .subject-top-left').html(subject_t_l)
        $('.subject-content .subject-top .subject-top-right').html(subject_t_r)
        $('#mainSubject').attr({
            'data-id':'mainSubject-' + special_data.vassetid,
            'data-routeid':special_data.onclick.route.tplID,
            'data-routetype':special_data.onclick.route.tplType,
            'data-href':special_data.onclick.href,
            'tabindex':'-1'
        }); 
        // 底部列表数据填充
        
        //fill_special_data(special_data.recommand)
        /*$.each(special_data.recommand, function(index, item) {
            console.log(item)
            $('#subjectul').append('<li><img src="'+item.img+'"><p class="style1-con-des">'+item.title+'</p></li>');
            $('#subjectul li').eq(index).attr({
                'data-id':'subjectul-' + item.vassetid,
                'data-routeid':item.onclick.route.tplID,
                'data-routetype':item.onclick.route.tplType,
                'data-href':item.onclick.href,
                'tabindex':'-1'
            }); 
        })*/
    };
    //解析专题页推荐位数据
    home_page.analysis_recommend_data = function(data){
        recommend_data = data.data.recommend; 
        return recommend_data;
    };
    // 生成专题推荐位数据
    home_page.create_recommend = function(special_data){
        //获取当前page
        //page = currentPage ? currentPage:0;
        
        //获取总页数
        console.log(special_data)
        totalPage = Math.ceil(special_data.length / 4);
        var currentSubSubject = special_data.slice(page * 4, page * 4 + 4);
        console.log(currentSubSubject)
        
        var leftSrc = page == 0?'http://127.0.0.1:8000/custom-templates/images/subject/zuojiantou_1.png':'http://127.0.0.1:8000/custom-templates/images/subject/zuojiantou.png';
        var rightSrc = page == totalPage-1?'http://127.0.0.1:8000/custom-templates/images/subject/youjiantou_1.png':'http://127.0.0.1:8000/custom-templates/images/subject/youjiantou.png';
        var str = "<div class='left_arrow'>\
                <img id='leftArrow' src='"+leftSrc+"'>\
            </div>\
            <div class='subject-list' id='subjectList'>\
                <ul id='subjectListUl'>"+createSubjectLi(currentSubSubject)+"</ul>\
            </div>\
            <div class='right_arrow'>\
                <img id='rightArrow' src='"+rightSrc+"'>\
            </div>"
            console.log(str)
        $("#specialRecommend").html(str);
    };
    function createSubjectLi(dataArray){

        var str = "";
        for(var i = 0; i < dataArray.length; i++){

            str += "<li tabindex='-1'><img src='"+dataArray[i].img+"'><p class='style1-con-des'>"+dataArray[i].title+"</p></li>"
        }
        return str;
    }
    //选择数据后填充页面
    function createSpecailLi(dataArray){
        var str = "";
        for(var i = 0; i < dataArray.length; i++){
            str += "<li tabindex='-1'><img src='"+dataArray[i].imgurl+"'><p class='style1-con-des'>"+dataArray[i].title+"</p></li>"
        }
        return str;
    }

    window.initSubject001 = function(){
        console.log('initSubject001')
        $("#custom-template-wrap").html(dom);
        home_page.init();  
    }
    //将选择后的数据填充对应区域
    window.fillArea = function(area, data) {
        data = JSON.parse(data);
        if(data){
            $("#specialRecommend").empty();
            //获取总页数
            totalPage = Math.ceil(data.length / 4);
            var currentSubSubject = data.slice(page * 4, page * 4 + 4);            
            var leftSrc = page == 0?'http://127.0.0.1:8000/custom-templates/images/subject/zuojiantou_1.png':'http://127.0.0.1:8000/custom-templates/images/subject/zuojiantou.png';
            var rightSrc = page == totalPage-1?'http://127.0.0.1:8000/custom-templates/images/subject/youjiantou_1.png':'http://127.0.0.1:8000/custom-templates/images/subject/youjiantou.png';
            var str = "<div class='left_arrow'>\
                    <img id='leftArrow' src='"+leftSrc+"'>\
                </div>\
                <div class='subject-list' id='subjectList'>\
                    <ul id='subjectListUl'>"+createSpecailLi(currentSubSubject)+"</ul>\
                </div>\
                <div class='right_arrow'>\
                    <img id='rightArrow' src='"+rightSrc+"'>\
                </div>"
            $("#specialRecommend").html(str); 
        }
    }
    function createSubjectLi(dataArray){

        var str = "";
        for(var i = 0; i < dataArray.length; i++){

            str += "<li tabindex='-1'><img src='"+dataArray[i].img+"'><p class='style1-con-des'>"+dataArray[i].title+"</p></li>"
        }
        console.log(dataArray)
        console.log(str)
        
        return str;
    }

})();